# -!- coding: utf-8 -!-
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
中移动项目的接口,提供对外
# 文件：api_msydlonline
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
import requests
from datetime import datetime
from interfacetest.baseset.msydonline_api import API_MSYD
from msydwork.config.config_msydonline import w_url
from pycacho.cachobase.logger import Logger
from pycacho.cachobase.deal_json import dict_to_json

logger = Logger("api_msydlonline").get_log()


headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.80 Safari/537.36',
            'content-Type': 'application/json',
        }

# /loan-sgw/v1/channel/jd-capital/act-capital-query
def act_capital_query(brwOrdNo,hbUsrNo,mblNo):
    fu_url = w_url + '/test/actCapitalQuery'
    data = API_MSYD['act_capital_query']
    data['brwOrdNo'] = brwOrdNo
    data['hbUsrNo'] = hbUsrNo
    data['mblNo'] = mblNo
    dict_to_json(data)
    req1 = requests.post(fu_url, json=data, headers=headers)
    data = req1.text
    logger.info("借款实际出资方预查询接口响应信息：%s", data)

# /loan-sgw/v1/channel/jd-capital/loan-apply
def loan_apply(brwOrdNo,mobileNo,idCardUserName,idCardNo,frontIdCardID,blackIdCardID,liveImageID,bankCardNo,bankMobileNo,provinceBillDate):
    fu_url = w_url + '/test/loanApply'
    data = API_MSYD['loan_apply']
    data['brwOrdNo'] = brwOrdNo
    data['mobileNo'] = mobileNo
    data['idCardUserName'] = idCardUserName
    data['userId'] = '99' + idCardNo
    data['idCardNo'] = idCardNo
    data['frontIdCardID'] = frontIdCardID
    data['blackIdCardID'] = blackIdCardID
    data['liveImageID'] = liveImageID
    data['bankCardNo'] = bankCardNo
    data['bankMobileNo'] = bankMobileNo
    data['provinceBillDate'] = provinceBillDate
    print("贷款申请接口请求参数：")
    dict_to_json(data)
    req1 = requests.post(fu_url, json=data, headers=headers)
    data = req1.text
    logger.info("贷款申请接口响应信息：%s", data)

# /loan-sgw/v1/channel/jd-capital/loan-result-query
def loan_result_query(brwOrdNo,brwOrdDt):
    fu_url = w_url + '/test/loanResultQuery'
    data = API_MSYD['loan_result_query']
    data['brwOrdNo'] = brwOrdNo
    data['brwOrdDt'] = brwOrdDt
    req1 = requests.post(fu_url, json=data, headers=headers)
    data = req1.text
    logger.info("贷款结果查询接口响应信息：%s", data)

# /loan-sgw/v1/channel/jd-capital/unfreeze
# brwOrdNo、brwOrdDt 唯一
def unfreeze(brwOrdNo,brwOrdDt):
    fu_url = w_url + '/test/unfreeze'
    data = API_MSYD['unfreeze']
    data['brwOrdNo'] = brwOrdNo
    data['brwOrdDt'] = brwOrdDt
    req1 = requests.post(fu_url, json=data, headers=headers)
    data = req1.text
    logger.info("资金方解冻额度接口响应信息：%s", data)

# /cmpay/quota/v1/block
def package_notify(brwOrdNo,orgOrderNo):
    fu_url = w_url + '/test/packageNotify'
    data = API_MSYD['package_notify']
    data['brwOrdNo'] = brwOrdNo
    data['orgOrderNo'] = orgOrderNo
    data['packageResult'] = 'S'          # 'F'
    print("业务办理通知接口请求参数：")
    dict_to_json(data)
    req1 = requests.post(fu_url, json=data, headers=headers)
    data = req1.text
    logger.info("业务办理通知接口响应信息：%s", data)

# /loan-sgw/v1/channel/jd-capital/repayment-plan-query
def repayment_plan_query(brwOrdNo):
    fu_url = w_url + '/test/repaymentPlanQuery'
    data = API_MSYD['repayment_plan_query']
    data['brwOrdNo'] = brwOrdNo
    req1 = requests.post(fu_url, json=data, headers=headers)
    data = req1.text
    logger.info("还款计划查询接口响应信息：%s", data)

# /loan-sgw/v1/channel/jd-capital/repayment
def repayment(brwOrdNo,orgOrdNo,repayModel,num):
    fu_url = w_url + '/test/repayment'
    print(fu_url)
    data = API_MSYD['repayment']
    data['brwOrdNo'] = brwOrdNo
    data['orgOrdNo'] = orgOrdNo
    data['repayModel'] = repayModel
    data['rpySeq'] = str(num)           # repayModel=1时必传,其余为空
    dict_to_json(data)
    req1 = requests.post(fu_url, json=data, headers=headers)
    data = req1.text
    logger.info("主动还款申请接口响应信息：%s", data)

# /loan-sgw/v1/channel/jd-capital/repayment-result-notify
def repayment_result_notify(brwOrdNo,orgOrdNo,repayType,num,pay_date):
    fu_url = w_url + '/test/repaymentResultNotify'
    data = API_MSYD['repayment_result_notify']
    data['brwOrdNo'] = brwOrdNo
    data['orgOrdNo'] = orgOrdNo
    data['repayOrdNo'] = 'r' + datetime.now().strftime('%Y%m%d%H%M%S')
    data['repayType'] = repayType             #
    data['repaySeq'] = str(num)             # 期数N，提前结清ALL
    data['repayAmount'] = '38.55'         # 51.25+ 1768.23
    data['repayOrdDt'] = pay_date
    dict_to_json(data)
    req1 = requests.post(fu_url, json=data, headers=headers)
    data = req1.text
    logger.info("还款结果通知接口响应信息：%s", data)

# /loan-sgw/v1/channel/jd-capital/returns
def returns(brwOrdNo):
    fu_url = w_url + '/test/returns'
    data = API_MSYD['returns']
    data['refundJrounalNo'] = 're' + datetime.now().strftime('%Y%m%d%H%M%S')
    data['brwOrdNo'] = brwOrdNo
    data['operateType'] = '1'            # 1-退货申请，2-退货查询
    data['refundType'] = '1'             # 1-退全部，2-退部分（暂时无）
    data['refundAmount'] = '700.20'      # 固定借款本金
    req1 = requests.post(fu_url, json=data, headers=headers)
    data = req1.text
    logger.info("退货申请接口响应信息：%s", data)

# test
def loan_update():
    fu_url = w_url + '/test/loan/update'
    req1 = requests.get(fu_url, headers=headers)
    data = req1.text
    logger.info("更新数据接口响应信息：%s", data)


if __name__ == '__main__':
    userId = '99432821197702284116'
    brwOrdNo = '50620201004125418689185119'
    orgOrderNo = 'CW20201004OL000038'
    mobileNo = '18807351662'
    idCardNo = '432821197702284116'
    #act_capital_query(brwOrdNo, userId, mobileNo)
    #loan_apply(brwOrdNo,mobileNo, userId, idCardNo,'','','')
    #loan_result_query(brwOrdNo,'20200510')
    #unfreeze(brwOrdNo,'20201119')
    #package_notify(brwOrdNo, orgOrderNo)
    #repayment_plan_query(brwOrdNo)
    repayment(brwOrdNo,orgOrderNo,'2','')     # # 还款模式 1-还某期,2-提前清贷,3-退货
    #repayment_result_notify(brwOrdNo, orgOrderNo, '1','1', '20201203')       # SEL-提前结清  FEE-退货手续
    #repayment_result_notify(brwOrdNo, orgOrderNo, '',1, '20201118')       # #   SEL-提前结清  FEE-退货手续
    #returns(brwOrdNo)
    #loan_update()

